<template>
    <el-card shadow="never" class="border-0">

        <!-- 新增|刷新 -->
        <div class="flex items-center  mb-4">
            <el-button type="primary" size="small" @click="clickAddBtn(0)">新增</el-button>
        </div>

        <el-table :data="tableData" 
            v-loading="tableDataLoading" 
            :border="true" 
            :tree-props="{ children: 'children' }"
            row-key="id" :indent="30">

            <el-table-column prop="name" label="课程类别" align="left"></el-table-column>

            <el-table-column label="操作" align="center" width="500px">
                <template #default="scope">
                    <div>
                        <el-button type="primary" size="small" text @click="clickEditBtn(scope.row)">修改</el-button>

                        <!-- <el-button v-if="scope.row?.children != null" type="primary" size="small" text
                            @click="clickAddBtn(scope.row.id)">新增</el-button> -->

                        <el-popconfirm title="是否要删除该类别？" confirmButtonText="确认" cancelButtonText="取消"
                            @confirm="clickDeleteBtn(scope.row.id)">
                            <template #reference>
                                <el-button text type="primary" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>

        <!-- 新增 更新 抽屉 -->
        <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="clickDrawerSubmitBtn">
            <el-form :model="form" ref="formRef" :rules="rules" label-width="120px" :inline="false">
                <el-form-item label="父级ID" prop="description">
                    <el-input v-model="form.parentId" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item required label="课程类别" prop="name">
                    <el-input v-model="form.name" placeholder="课程类别"></el-input>
                </el-form-item>
            </el-form>
        </FormDrawer>
    </el-card>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from 'vue'
import { api_category_tree, api_category_add, api_category_update, api_category_delete } from "~/api/exam/categoryApi";
import FormDrawer from '~/components/FormDrawer.vue'
import { toast } from "~/composables/util"

// 页面变量
const tableData = ref([]) // 列表数据
const tableDataLoading = ref(false)
// 校验
const rules = {
    name: [
        { required: true, message: '名称不能为空', trigger: 'change' }
    ]
}
const editId = ref(0)
const drawerTitle = computed(() => editId.value ? "修改" : "新增")
const form = reactive({
    id: editId.value,
    parentId: 0,
    name: null
})
const formRef = ref(null)
const formDrawerRef = ref(null)

// 生命周期
onMounted(() => {
    //初始化数据
    getData()
})

//获取列表数据
function getData(p = null) {
    tableDataLoading.value = true
    api_category_tree()
        .then(res => {
            tableData.value = res
        })
        .finally(() => {
            tableDataLoading.value = false
        })
}

// 重置表单
function resetForm(row = false) {
    if (formRef.value) formRef.value.resetFields()
    if (row) {
        for (const key in form) {
            form[key] = row[key]
        }
    }
}

// 新增|修改
const clickDrawerSubmitBtn = () => {
    formRef.value.validate((valid) => {
        if (!valid) return
        formDrawerRef.value.showLoading()
        const fun = editId.value ? api_category_update(form) : api_category_add(form)
        fun.then(res => {
            toast(drawerTitle.value + "成功")
            // 修改刷新当前页，新增刷新第一页
            getData(editId.value ? false : 1)
            formDrawerRef.value.close()
        }).catch(err => {

        }).finally(() => {
            formDrawerRef.value.hideLoading()
        })
    })
}

// 编辑
const clickEditBtn = (row) => {
    editId.value = row.id
    resetForm(row)
    formDrawerRef.value.open()
}

// 新增
const clickAddBtn = (parentId) => {
    //修改标记置为0
    editId.value = 0
    resetForm({
        parentId: parentId,
        name: null,
    })
    formDrawerRef.value.open()
}

// 删除
const clickDeleteBtn = (id) => {
    tableDataLoading.value = true
    api_category_delete(id).then(res => {
        toast("删除成功")
        getData()
    }).catch(err => {

    }).finally(() => {
        tableDataLoading.value = false
    })
}
</script>